#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest

from openchord import URL, BadURLError


class   URL01TestCase(unittest.TestCase) :
    def setUp(self) :
        pass
        ## setUp()

    def tearDown(self) :
        pass
        ## tearDown()

    def testParse(self) :
        url_string = 'octcp://127.0.0.1:1234/path/to/func'
        url = URL(url_string)
        self.assertEqual(url.protocol, 'octcp')
        self.assertEqual(url.host, '127.0.0.1')
        self.assertEqual(url.port, 1234)
        self.assertEqual(url.path, 'path/to/func')

        url_string = 'octcp://127.0.0.1/path/to/func'
        url = URL(url_string)
        self.assertEqual(url.protocol, 'octcp')
        self.assertEqual(url.host, '127.0.0.1')
        self.assertEqual(url.port, URL.TCP_PORT_DEFAULT)
        self.assertEqual(url.path, 'path/to/func')
        ## testParse()

    def testEqual(self) :
        url_string_1 = 'octcp://127.0.0.1/path/to/func'
        url_string_2 = 'octcp://127.0.0.1:4344/path/to/func'
        url1 = URL(url_string_1)
        url2 = URL(url_string_2)
        self.assertEqual(url1, url2)

        url_string_3 = 'octcp://127.0.0.1/path/to/FUNC'
        url_string_4 = 'octcp://127.0.0.1:4344/path/to/func'
        url3 = URL(url_string_3)
        url4 = URL(url_string_4)
        self.assertNotEqual(url3, url4)

        url_string_5 = 'octcp://127.0.0.1:1234/path/to/func'
        url_string_6 = 'octcp://127.0.0.1:4344/path/to/func'
        url5 = URL(url_string_5)
        url6 = URL(url_string_6)
        self.assertNotEqual(url5, url6)
        ## testEqual()

    def testMalformedURL(self) :
        url_string = 'http://abc.cn' 
        self.assertRaises(BadURLError, URL, url_string)
        url_string = 'octcp://abc.cn' 
        self.assertRaises(BadURLError, URL, url_string)
        url_string = 'octcp://abc.cn:0/' 
        self.assertRaises(BadURLError, URL, url_string)
        url_string = 'octcp://abc.cn:65537/' 
        self.assertRaises(BadURLError, URL, url_string)
        url_string = 'octcp://abc.cn:-100/'
        self.assertRaises(BadURLError, URL, url_string)
        url_string = 'http://abc.cn:1234/' 
        self.assertRaises(BadURLError, URL, url_string)
        ## testMalformedURL()

    ## class URL01TestCase

if __name__ == '__main__' :
    unittest.main()

